package com.google.api.adwords.starterkit.Synchronizer;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashSet;

import com.google.api.adwords.starterkit.settings.StarterKitSettings;
import com.google.api.adwords.starterkit.utils.DumpExceptions;
import com.google.api.adwords.v201101.cm.Campaign;
import com.google.api.adwords.v201101.cm.CampaignOperation;
import com.google.api.adwords.v201101.cm.NetworkSetting;
import com.google.api.adwords.v201101.cm.Operator;

/**
 * The Class NetworkTargetSynchronizer is responsible to keep all NetworkTarget 
 * information from AdWords system synchronized with local DB.
 * 
 * @author rodrigo.f.vale@gmail.com (Rodrigo Vale)
 */
public class NetworkTargetSynchronizer extends CampaignSynchronizer {

  /**
   * Instantiates a new synchronizer.
   * 
   * @param serviceUtils
   *          the service utils to access AdWords system
   * @param starterKitSetting
   *          the starterkit settings
   * @param conn
   *          the connection to local db
   */
  public NetworkTargetSynchronizer(StarterKitSettings starterKitSetting, 
      Connection conn) {
    super(starterKitSetting, conn);
  }

  /**
   * Process the updates in networkTarget table.
   */
  public void put() {
    log.debug("Preparing NetworkTarget to be sent to AdWords");
    networkTargetTable.updateCampaignsIds();
  
    ResultSet rs = networkTargetTable.getModified();
    try {
      HashSet<CampaignOperation> campaignOperations 
          = new HashSet<CampaignOperation>();
      long lastCampaignId = 0;
      long campaignId = 0;
      String network = "";
      Campaign campaign = null;
      NetworkSetting networkSetting = null;
  
      while (rs.next()) {
        campaignId = rs.getLong("CampaignId");
        network = rs.getString("Network");
        if (lastCampaignId != campaignId) {
          campaign = new Campaign();
          campaign.setId(campaignId);
          networkSetting = new NetworkSetting();
          networkSetting.setTargetContentContextual(false);
          networkSetting.setTargetContentNetwork(false);
          networkSetting.setTargetSearchNetwork(false);
          networkSetting.setTargetGoogleSearch(false);
          campaign.setNetworkSetting(networkSetting);
  
          CampaignOperation campaignOperation = new CampaignOperation();
          campaignOperation.setOperand(campaign);
          campaignOperation.setOperator(Operator.SET);
          campaignOperations.add(campaignOperation);
  
          lastCampaignId = campaignId;
        }
  
        if (network.equals("CONTENT_CONTEXTUAL")) {
          networkSetting.setTargetContentContextual(true);
        } else if (network.equals("CONTENT_NETWORK")) {
          networkSetting.setTargetContentNetwork(true);
        } else if (network.equals("SEARCH_NETWORK")) {
          networkSetting.setTargetSearchNetwork(true);
        } else if (network.equals("GOOGLE_SEARCH")) {
          networkSetting.setTargetGoogleSearch(true);
        }
      }
      rs.close();  
      processOperations(campaignOperations);
    } catch (SQLException e) {
      DumpExceptions.dumpSQLException(e);
    }    
    campaignTable.resetNetworkTargetFlags();
  }

}
